package com.woniu.adapter;

import com.woniu.outlet.mysql.MessageDao;
import com.woniu.outlet.mysql.po.MessageMySQL;
import com.woniu.outlet.mysql.po.MessageMySQLExample;
import com.woniu.service.bean.MessageReceivedCommand;
import com.woniu.service.bean.SendMessageCommand;
import com.woniu.service.domain.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class MessageRepositoryAdapter {

    @Autowired
    private MessageDao messageDao;

    public void insert(SendMessageCommand command) {

        MessageMySQL po = MessageMySQL.builder()
                .exchange(command.getExchange())
                .routingKey(command.getRoutingKey())
                .messageContent(command.getMessageContext())
                .retryCount(5)
                .status(Message.MESSAGE_STATUS_UNSEND)
                .build();

        messageDao.insert(po);
    }

    /**
     * 查找符合发送发送标准的消息：
     *  status !="已接收" 且 retryCount > 0
     */
    public List<Message> selectMessageForSend() {

        MessageMySQLExample example = new MessageMySQLExample();
        example.or()
                .andStatusNotEqualTo(Message.MESSAGE_STATUS_RECEIVED)
                .andRetryCountGreaterThan(0);

        List<MessageMySQL> poList = messageDao.selectByExample(example);

        return convertToMessageList(poList);
    }

    public void updateMessageRetryCount(Message message) {
        MessageMySQL po = MessageMySQL.builder()
                .id(message.getId())
                .retryCount(message.getRetryCount())
                .build();
        messageDao.updateByPrimaryKeySelective(po);
    }

    /**
     * 将消息的状态改为【已接收】
     */
    public void changeMessageStatusToReceived(Integer id) {
        MessageMySQL po = MessageMySQL.builder()
                .id(id)
                .status(Message.MESSAGE_STATUS_RECEIVED)
                .build();
        messageDao.updateByPrimaryKeySelective(po);
    }

    /**
     * 将消息的状态改为【已发送】
     */
    public void changeMessageStatusToSended(Integer id) {
        MessageMySQL po = MessageMySQL.builder()
                .id(id)
                .status(Message.MESSAGE_STATUS_SENDED)
                .build();
        messageDao.updateByPrimaryKeySelective(po);
    }


    /**
     * poList 转 domainList
     */
    private List<Message> convertToMessageList(List<MessageMySQL> poList) {
        List<Message> domainList = new ArrayList<>();
        for (MessageMySQL po : poList) {
            Message domain = convertToMessage(po);
            domainList.add(domain);
        }

        return domainList;
    }

    /**
     * po 转 domain
     */
    private Message convertToMessage(MessageMySQL po) {
        return Message.builder()
                .id(po.getId())
                .exchange(po.getExchange())
                .routingKey(po.getRoutingKey())
                .messageContent(po.getMessageContent())
                .status(po.getStatus())
                .retryCount(po.getRetryCount())
                .build();
    }
}
